package com.lsu.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsu.app.bean.Resource;
import com.lsu.app.service.ResourceService;
import com.lsu.app.mapper.ResourceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @description 针对表【resource(图片/视频文件)】的数据库操作Service实现
 * @Date 2023-10-20 18:13:30
 * @author hjj
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService{
    @Value("${web.assess-path}")
    private String assessPath;

    @Value("${web.upload-path}")
    private String uploadPath;

    @Autowired
    private HttpServletRequest request;
    private final ResourceMapper mapper;

    public ResourceServiceImpl(ResourceMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public String getCourseNameByVideoId(Integer id) {
        return mapper.getCourseNameByVideoId(id);
    }

    @Override
    public List<Integer> getReIdsByTypeAndUorrId(Integer type, Integer id) {
        return mapper.getReIdsByTypeAndUorrId(type, id);
    }

    @Override
    public Resource getResourceByUsername(String username) {
        return mapper.getResourceByUsername(username);
    }

    @Override
    public String getHeadUrlByUorrIdAndType(Integer userId) {
        return assessPath+mapper.getHeadUrlByUorrIdAndType(userId);
    }

    @Override
    public String getVideoUrlByUorrIdAndId(Integer uorrId,Integer id) {
        return assessPath+mapper.getTeacherIdByCourseId(uorrId)+"/"+uorrId+"/"+mapper.getVideoUrlByUorrIdAndId(id);
    }

    @Override
    public Integer getUorrIdById(Integer id) {
        return mapper.getUorrIdById(id);
    }

    @Override
    public List<Integer> getIdsByCourseId(Integer courseId) {
        return mapper.getIdsByCourseId(courseId);
    }

    @Override
    public Integer getEpisodesByUorrId(Integer id) {
        Integer count = mapper.getEpisodesByUorrId(id);
        return count!=null?count:1;
    }

    @Override
    public String getCoverImgUrlByCourseId(Integer id) {
        return assessPath+mapper.getTeacherIdByCourseId(id)+"/"+id+"/"+mapper.getCoverImgUrlByCourseId(id);
    }

    @Override
    public void adjustEpisodes(Integer episodes) {
        mapper.adjustEpisodes(episodes);
    }

    @Override
    public String[] getTalkImgsUrlByTalkId(Integer talkId) {
        List<String> orurls = mapper.getTalkImgsUrlByTalkId(talkId);
        String[] urls = new String[orurls.size()];
        for (int i = 0; i < orurls.size(); i++) {
             urls[i] = assessPath+"talks/"+orurls.get(i);
        }
        return urls;
    }

    @Override
    public String getTeacherNameByVideoId(Integer uorrId) {
        return mapper.getTeacherNameByVideoId(uorrId);
    }

    @Override
    public Integer getCountOfRemake(String remake, Integer courseId) {
        return mapper.getCountOfRemake(remake,courseId);
    }

    @Override
    public String getUploadImg(Integer uorrId) {
        return uploadPath+mapper.getCoverImgUrlByCourseId(uorrId);
    }

    @Override
    public String getCourseCoverUrlByVideoId(Integer id) {
        Integer courseId = mapper.getUorrIdById(id);
        return assessPath+mapper.getTeacherIdByVideoId(id)+"/"+courseId+"/"+mapper.getCourseCoverUrlByVideoId(courseId);
    }

    @Override
    public Integer getTeacherIdByVideoId(Integer id) {
        return mapper.getTeacherIdByVideoId(id);
    }
}




